
package com.funo.increment;

import com.scj.testndk.HelloJni;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.File;

public class TestNDK extends Activity {

    private TextView showMessage;

    private Button button;

    // private Button button2;

    private MyBroadcastReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showMessage = (TextView) findViewById(R.id.showMessage);

        myReceiver = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(MyContant.DOWN_PROCESS_ACTION);
        this.registerReceiver(myReceiver, filter);

        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                beginApply2();

            }
        });

        // button2 = (Button) findViewById(R.id.button2);
        //
        // button2.setOnClickListener(new View.OnClickListener() {
        //
        // @Override
        // public void onClick(View v) {
        // beginApply2();
        //
        // }
        // });

    }

    @Override
    protected void onDestroy() {
        this.unregisterReceiver(myReceiver);
        super.onDestroy();

    }

    private void beginApply2() {

        String packageName = "com.funo.commhelper";
        MyUtil.startDownServiceOther(this, "http://t.cn/zj63czF", packageName, packageName + ".apk");

    }

    private void beginApply() {
        try {

            String packageName = "com.funo.commhelper";
            String localApkPath = MyUtil.getAPkPathByPackageName(this, packageName);

            File file = MyUtil.copyInstallApkToFile(localApkPath, packageName + ".apk");
            if (file == null) {
                showMessage.setText("拷贝文件异常");
                return;
            }

            HelloJni diff = new HelloJni();
            String oldFilePath = file.getPath();

            // MyContant.PRX + "NetTest_FJ_v3.3.13.apk";

            String newFilePath_hc = MyContant.PRX + packageName + "_hc.apk";

            String patchFilePath = MyContant.PRX + "CommHelperV2.1_V2.1.1.apk.patch";

            File fileOld = new File(oldFilePath);
            File filePath = new File(patchFilePath);
            showMessage.setText("基础文件是否存在 " + fileOld.exists() + " path 文件是否存在 "
                    + filePath.exists());

            String result = diff.applyPatch(oldFilePath, newFilePath_hc, patchFilePath);

            showMessage.setText(result);
        } catch (Exception e) {
            showMessage.setText("测试异常");
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent != null && intent.getAction().equals(MyContant.DOWN_PROCESS_ACTION)) {
                String process = intent.getStringExtra(MyContant.DOWN_PROCESS);
                String total = intent.getStringExtra(MyContant.DOWN_PROCESS_TOTAL);
                boolean success = intent.getBooleanExtra(MyContant.SUCCESS,false);
                showMessage.setText("文件已经下载：" + process + " 总大小：" + total+" 执行结果"+success);
            }
        }
    }

}
